﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyDay.Models;

namespace MyDay.Controllers
{

    [Authorize]
    public class TodosController : Controller
    {
        private MyDay.Models.Todo.TodosContext db = new MyDay.Models.Todo.TodosContext();

        //
        // GET: /Todos/

        public ActionResult Index()
        {
            var context = new UsersContext();
            var username = User.Identity.Name;
            var user = context.UserProfiles.SingleOrDefault(u => u.UserName == username);
            Session["FullName"] = user.FullName;
            Session["UserId"] = user.UserId;

            var results = db.Todos.Where(x => x.UserProfile.UserId == user.UserId).ToList();
            if (results == null)
                results = new List<Todo>();
            return View(results);
        }

        //
        // GET: /Todos/Details/5

        public ActionResult Details(int id = 0)
        {
            Todo todo = db.Todos.Find(id);
            if (todo == null)
            {
                return HttpNotFound();
            }
            return View(todo);
        }

        //
        // GET: /Todos/Create

        public ActionResult Create()
        {
            var todo = new Todo();
            todo.DueDate = DateTime.Now;
            return View(todo);
        }

        //
        // POST: /Todos/Create

        [HttpPost]
        public ActionResult Create(Todo todo)
        {
            if (ModelState.IsValid)
            {
                db.Todos.Add(todo);
                db.SaveChanges();
                Session["FlashSuccess"] = "The todo record has been added.";
                return RedirectToAction("Index");
            }

            return View(todo);
        }

        //
        // GET: /Todos/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Todo todo = db.Todos.Find(id);
            if (todo == null)
            {
                return HttpNotFound();
            }
            return View(todo);
        }

        //
        // POST: /Todos/Edit/5

        [HttpPost]
        public ActionResult Edit(Todo todo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(todo).State = EntityState.Modified;
                db.SaveChanges();
                Session["FlashSuccess"] = "The todo record has been saved.";
                return RedirectToAction("Index");
            }
            return View(todo);
        }

        //
        // GET: /Todos/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Todo todo = db.Todos.Find(id);
            if (todo == null)
            {
                return HttpNotFound();
            }
            return View(todo);
        }

        //
        // POST: /Todos/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Todo todo = db.Todos.Find(id);
            db.Todos.Remove(todo);
            db.SaveChanges();
            Session["FlashSuccess"] = "The todo record has been deleted.";
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}